<template>
  <div>
<!--  面包屑  -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>竞赛活动</el-breadcrumb-item>
      <el-breadcrumb-item>公告管理</el-breadcrumb-item>
    </el-breadcrumb>
<!--  搜索框  -->
    <div style="margin-top: 20px;">
      <el-input placeholder="请输入内容" v-model="input" class="input-with-select">
        <el-select v-model="select" slot="prepend" placeholder="请选择" style="width: 130px">
          <el-option label="日期" value="date"></el-option>
          <el-option label="标题" value="name"></el-option>
          <el-option label="赛事" value="father"></el-option>
        </el-select>
        <el-button slot="append" icon="el-icon-search" @click="searchCompetition"></el-button>
      </el-input>
    </div>
<!--  表格内容  -->
    <el-table
      :data="tableData"
      style="width: 100% ;height: 100%;margin-top: 10px">
      <el-table-column
        fixed="left"
        prop="id"
        label="序号"
        width="150">
      </el-table-column>
      <el-table-column
        prop="date"
        label="日期"
        width="200">
      </el-table-column>
      <el-table-column
        prop="name"
        label="标题"
        width="350">
      </el-table-column>
      <el-table-column
        prop="father"
        label="相关赛事"
        width="200">
      </el-table-column>
      <el-table-column
        fixed="right"
        label="执行操作"
        width="250"
      >
        <template slot-scope="scope">
          <el-button
            @click="edit(scope.row)"
            size="mini">
            编辑
          </el-button>
          <!--      scope.row能返回该行的所有数据信息      -->
          <el-button
            @click.native.prevent="deleteRow(scope.row.id,scope.$index, tableData)"
            type="danger"
            size="mini">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
<!--    分页  很多方法都内置好了-->
    <div class="block">
      <!--        必须要用sync，否则current-page无法改变-->
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage"
        :page-sizes="[8, 16, 24, 32]"
        v-bind:page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="50">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import axios from 'axios'
import Tinymce from '../components/Tinymce'

export default {
  components: {Tinymce},
  methods: {
    // 编辑按钮，会跳转至competitionEdit，同时携带该行信息
    edit (row) {
      this.$router.push({
        name: 'competitionEdit',
        query: {
          row: row
        }
      })
    },
    // 删除按钮
    deleteRow (id, index, rows) {
      this.$confirm('此操作将删除该活动, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 在then()中进行删除操作
        // 先在前端页面进行删除
        rows.splice(index, 1)
        this.$axios({
          url: 'test/competition/delete',
          method: 'post',
          data: {id}
          //  一定要用json格式的数据！！！！！！！！！！！
        })
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 根据参数查找表格数据
    searchCompetitionAll (input = '', select = '') {
      // 初始URL
      // eslint-disable-next-line no-unused-vars
      var url = 'test/data_test/getModelData'
      // 带参数URL，无参数表示查询所有
      if (input !== '' && select !== '') {
        url = url + '?input=' + input + '&select=' + select
      }
      // 请求表格数据
      // http://localhost/tp5.1test/public/test/data_test/getModelData
      axios.get(url).then(res => {
        this.tableData = res.data
      }).catch(err => {
        console.log(err)
      })
    },
    // 根据搜索内容查找
    searchCompetition () {
      this.searchCompetitionAll(this.input, this.select)
    },
    // 分页功能
    handleSizeChange (val) {
      this.pagesize = val
      this.currentPage = 1
      this.$axios({
        url: 'test/competition/paginate',
        method: 'post',
        // headers: { Authorization: localStorage.getItem('tk') },
        data: {
          pagesize: this.pagesize
        }
      }).then(res => {
        this.tableData = res.data.data
      })
    },
    // 分页功能
    handleCurrentChange (val) {
      // console.log(val)
      // console.log(this.currentPage) 两者一样
      this.$axios({
        url: 'test/competition/paginate2',
        method: 'post',
        // headers: { Authorization: localStorage.getItem('tk') },
        data: {
          pagecount: val,
          pagesize: this.pagesize
        }
      }).then(res => {
        this.tableData = res.data
      })
    }
  },
  data () {
    return {
      // 面包屑数据
      input: '',
      select: '',
      // 表格数据
      tableData: [{
        content: '',
        date: '',
        father: '',
        id: '',
        name: ''
      }],
      //  编辑弹出框中的表单数据
      // type数据暂时无法更新，但是控制台输出有值，暂未解决！！！！！！！！
      dialogFormVisible2: false,
      sizeForm2: {
        name: '',
        content: '',
        tpye: '',
        date: '',
        id: ''
      },
      //  分页数据  当前第几页，当前每页数量
      currentPage: 1,
      pagesize: 8
    }
  },
  mounted () {
    // this.searchCompetitionAll()
    // 进入后8条/页，而不是直接显示所有数据
    this.handleSizeChange(this.pagesize)
  }
}
</script>
<style>

.input-with-select .el-input-group__prepend {
  background-color: #fff;
}

</style>
